import getspu from '@/api/spu'
const state = {
  spuList: [],
  BaseSaleAttrList: [],
  TrademarkList: [],
  spuImageList:[],
  trademarkData:[],
  id3:''
}

const mutations = {
  SET_SPU_LIST(state, data) {
    state.spuList = data
  },
  GET_BASES_ALEATTRLIST(state,data){   //固定的3个销售属性
    state.BaseSaleAttrList = data
  },
  GET_TRADEMARKLIST(state,data){
    state.TrademarkList = data
  },
  GET_SPUIMAGELIST(state,data){
    state.spuImageList = data
  },
  GET_SPUSALEATTRLIST(state,data){  //请求得到的销售属性
    state.trademarkData = data
  },
  ADD_SPUSALEATTRLIST(state,data){   //自己添加的销售属性
    state.trademarkData.push(data)
  },
  SET_ID3(state,data){
    state.id3 = data
  },
  RESET_SPULIST(state){  //清空某些数据
    state.spuList = []
    state.spuImageList = []
    state.trademarkData = []
  },
  SIFT_BASESALEATTRLIST(state){    //筛选BaseSaleAttrList
    state.BaseSaleAttrList = state.BaseSaleAttrList.filter((item)=>{
      return !(state.trademarkData.some((i)=>{
        return i.saleAttrName === item.name
      }))
    })
  }
}

const actions = {
  async getSpuList({ commit }, {id3,page,size}) {
    const res = await getspu.getSpuList(id3,page,size)
    // console.log(res)
    if (res.code === 200) {
      commit('SET_SPU_LIST', res.data)
    }
  },
  async getBaseSaleAttrList({ commit }) {  //固定的3个销售属性
    const res = await getspu.getBaseSaleAttrList()
    console.log(res)
    if (res.code === 200) {
      commit('GET_BASES_ALEATTRLIST',res.data)
    }
  },
  async getTrademarkList({ commit }) {
    const res = await getspu.getTrademarkList()
    // console.log(res)
    if (res.code === 200) {
      commit('GET_TRADEMARKLIST',res.data)
    }
  },
  async getSpuImageList({ commit },id){   //图片
    const res = await getspu.getSpuImageList(id)
    // console.log(res)
    if (res.code === 200) {
      commit('GET_SPUIMAGELIST',res.data)
    }
  },
  async getSpuSaleAttrList({ commit },id){   //spu属性值tag
    const res = await getspu.getSpuSaleAttrList(id)
    console.log(res)
    if (res.code === 200) {
      commit('GET_SPUSALEATTRLIST',res.data)
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}